package top.myhdg.bijou.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class BookmarkDao {

    private final String[] BOOKMARK_COLUMNS = new String[]{"Website", "Title"};

    private BookmarkDBHelper dbHelper;

    public BookmarkDao(Context mContext) {
        dbHelper = new BookmarkDBHelper(mContext);
    }

    //=====判断表中是否有数据=====
    public boolean isDataExist() {
        int count = 0;

        SQLiteDatabase db = null;
        Cursor cursor = null;

        try {
            db = dbHelper.getReadableDatabase();
            cursor = db.query(BookmarkDBHelper.TABLE_NAME, new String[]{"COUNT(Website)"}, null, null, null, null, null);

            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
            if (count > 0) return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return false;
    }

    //=====获取所有数据=====
    public List<Bookmark> getAllDate() {
        SQLiteDatabase db = null;
        Cursor cursor = null;

        try {
            db = dbHelper.getReadableDatabase();
            cursor = db.query(BookmarkDBHelper.TABLE_NAME, BOOKMARK_COLUMNS, null, null, null, null, null);

            if (cursor.getCount() > 0) {
                List<Bookmark> bookmarkList = new ArrayList<>(cursor.getCount());
                while (cursor.moveToNext()) {
                    bookmarkList.add(parseBookmark(cursor));
                }
                return bookmarkList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }

        return null;
    }

    //=====将数据转换为Bookmark类=====
    private Bookmark parseBookmark(Cursor cursor) {
        Bookmark bookmark = new Bookmark();
        bookmark.setTitle(cursor.getString(cursor.getColumnIndex("Title")));
        bookmark.setWebsite(cursor.getString(cursor.getColumnIndex("Website")));
        return bookmark;
    }

    //=====插入一条数据=====
    public void insertDate(Bookmark bookmark) {
        SQLiteDatabase db = null;

        String title = bookmark.getTitle();
        String website = bookmark.getWebsite();
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();

            ContentValues contentValues = new ContentValues();
            contentValues.put("Website", website);
            contentValues.put("Title", title);
//            db.insertOrThrow(BookmarkDBHelper.TABLE_NAME, null, contentValues);
            db.replaceOrThrow(BookmarkDBHelper.TABLE_NAME, null, contentValues);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }

    //=====删除一条数据=====
    public void deleteBookmark(Bookmark bookmark) {
        SQLiteDatabase db = null;

        String website = bookmark.getWebsite();
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();
            db.delete(BookmarkDBHelper.TABLE_NAME, "Website = ?", new String[]{website});
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }

    //=====修改数据=====
    public void updateBookmark(Bookmark oldBookmark, Bookmark newBookmark) {
        SQLiteDatabase db = null;

        String oldTitle = oldBookmark.getTitle();
        String oldWebsite = oldBookmark.getWebsite();
        String newTitle = newBookmark.getTitle();
        String newWebsite = newBookmark.getWebsite();
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();

            ContentValues cvTitle = new ContentValues();
            cvTitle.put("Title", newTitle);
            ContentValues cvWebsite = new ContentValues();
            cvWebsite.put("Website", newWebsite);
            db.update(BookmarkDBHelper.TABLE_NAME,
                    cvTitle,
                    "Title = ?",
                    new String[]{oldTitle});
            db.update(BookmarkDBHelper.TABLE_NAME,
                    cvWebsite,
                    "Website = ?",
                    new String[]{oldWebsite});
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }
}
